// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import FastClick from 'fastclick'
import VueRouter from 'vue-router'
import App from './App'
import { sync } from 'vuex-router-sync'
import XMixin from './XMixin'

import 'whatwg-fetch'

// String.prototype.parseLineChar = function () {
//   return this.replace(/\n/g, "<br />")
// }

Vue.use(VueRouter)

import Vuex from 'vuex'
Vue.use(Vuex)

import routes from './router'
console.log(routes)

const router = new VueRouter({
  routes
})

import helper from 'lanmaLib/helper'
import api from 'lanmaLib/common/api'
import config from 'lanmaLib/config'
import label from 'lanmaLib/modules/label'
import user from 'lanmaLib/modules/user'
import course from 'lanmaLib/modules/course'
import material from 'lanmaLib/modules/material'

import common from './common'

Vue.prototype.$helper = helper
Vue.prototype.$api = api
Vue.prototype.$config = config
Vue.prototype.$label = label
Vue.prototype.$user = user
Vue.prototype.$course = course
Vue.prototype.$material = material
Vue.prototype.$common = common

import store from './store'
sync(store, router)

Vue.prototype.$t = function (text) {
  return text
}

import { cookie, DevicePlugin, ToastPlugin, AlertPlugin, LoadingPlugin, ConfirmPlugin, WechatPlugin, AjaxPlugin } from 'vux'

window.authUser = function () {
  api.setToken('')
  // var roleId = 0
  // if (window.location.href.indexOf('/agency') !== -1) {
  //   roleId = 4
  // } else {
  //   roleId = 3
  // }
  var jump = encodeURIComponent(window.location.href)
  var source = common.getParamter('source')
  var authUrl = `http://api.xinjijiaoyu.com/wechat/auth?jump=${jump}&source=${source}`
  console.log(authUrl)
  window.location.href = authUrl
}

window.xLog = function (log) {
  var data = {log}
  window.fetch('/api/logs', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
  })
}

window.onerror = function (e) {
  window.xLog(e)
}

function loadUserInfo () {
  user.getUserInfo()
    .then((userInfo) => {
      console.log('getUserInfo')
      console.log(userInfo)
      // store.dispatch('userLogin', userInfo)
    }).catch((e) => {
      // window.authUser()
      console.log(e)
    })
}

Vue.prototype.$xLog = window.xLog

window.xLog('start login check')
var isLogin = user.checkLogin()
window.xLog(api.getToken())
if (!isLogin) {
  var token = cookie.get('token')
  window.xLog(token)
  console.log(token)
  if (token) {
    api.setToken(token)
    loadUserInfo()
  } else {
    window.authUser()
  }
} else {
  loadUserInfo()
  user.dayLoginLog()
}

// 拉取标签数据
label.getLabelList()

Vue.use(DevicePlugin)
Vue.use(ToastPlugin)
Vue.use(AlertPlugin)
Vue.use(ConfirmPlugin)
Vue.use(LoadingPlugin)
Vue.use(WechatPlugin)
Vue.use(AjaxPlugin)

const wx = WechatPlugin.$wechat
const http = AjaxPlugin.$http

wx.ready(() => {
  console.log('wechat ready')
  var url = window.location.href
  if (url.indexOf('?') === -1) {
    url += '?'
  }
  var uid = common.getUid()
  url += 'source=' + uid
  wx.onMenuShareAppMessage({
    title: '懒妈学院', // 分享标题
    desc: '懒妈学院',
    link: url,
    imgUrl: 'http://wx.xinjijiaoyu.com/static/img/logo.jpg'
  })

  wx.onMenuShareTimeline({
    title: '懒妈学院', // 分享标题
    desc: '懒妈学院',
    link: url,
    imgUrl: 'http://wx.xinjijiaoyu.com/static/img/logo.jpg'
  })
})

const permissions = JSON.stringify(['onMenuShareTimeline', 'onMenuShareAppMessage', 'chooseImage', 'uploadImage'])
const url = document.location.href
http.get('http://wx.xinjijiaoyu.com/api/wechat/jssdk?url=' + encodeURIComponent(url.split('#')[0]) + '&jsApiList=' + permissions).then(res => {
  console.log(res)
  wx.config(res.data)
})

FastClick.attach(document.body)

Vue.mixin(XMixin)

/* eslint-disable no-new */
new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app')
